/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 09
 * Time: 18:00
 */
class Solution {
    public ListNode removeZeroSumSublists(ListNode head) {
        if(head == null) return null;
        ListNode dummyHead = new ListNode(0);//创建一个头结点作为要处理的链表的虚拟头结点
        dummyHead.next = head;
        ListNode pre = dummyHead;
        while(pre != null) {
            ListNode p = pre.next;
            int sum = 0;
            while(p != null) {
                sum += p.val;
                if(sum == 0) {
                    pre.next = p.next;
                    break;
                }
                p = p.next;
            }
            if(p == null) pre = pre.next;//这一步只是为了跳出最外层的for循环,加入(p==null)这个条件是为了处理要处理的链表的头结点为0这种情况
        }
        return dummyHead.next;
    }
}
